package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.presenter.FoodtogoSettingPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
* Created by Hu on 2016/12/14
*/

public class FoodtogoSettingModelImpl implements FoodtogoSettingModel, VolleyJsonObjectCallBack {
    FoodtogoSettingPresenterImpl foodtogoSettingPresenter;
    @Override
    public void sendUrl(String url, FoodtogoSettingPresenterImpl foodtogoSettingPresenter) {
        this.foodtogoSettingPresenter=foodtogoSettingPresenter;
        volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void getMsgUrl(String url, FoodtogoSettingPresenterImpl foodtogoSettingPresenter) {
        this.foodtogoSettingPresenter=foodtogoSettingPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    RestaurantInfo restaurantInfo= JsonUtil.getObjectFromString(response.getString("res"),RestaurantInfo.class);
                    foodtogoSettingPresenter.recMsg(restaurantInfo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            foodtogoSettingPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            int res =response.getInt("res");
            foodtogoSettingPresenter.recRes(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
            foodtogoSettingPresenter.sendError(error);
    }
}